home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / g_quake / ultqsrc.zip / CMDLIB.C < prev    next >
C/C++ Source or Header  |  1996-07-25  |  9KB  |  599 lines

  1. // cmdlib.c
  2.  
  3. #include "cmdlib.h"
  4. #include <sys/time.h>
  5.  
  6. #define PATHSEPERATOR   '/'
  7.  
  8. // set these before calling CheckParm
  9. int myargc;
  10. char **myargv;
  11.  
  12. char    com_token[1024];
  13. int        com_eof;
  14.  
  15. /*
  16. ================
  17. I_FloatTime
  18. ================
  19. */
  20. double I_FloatTime (void)
  21. {
  22.     struct timeval tp;
  23.     struct timezone tzp;
  24.     static int        secbase;
  25.  
  26.     gettimeofday(&tp, &tzp);
  27.     
  28.     if (!secbase)
  29.     {
  30.         secbase = tp.tv_sec;
  31.         return tp.tv_usec/1000000.0;
  32.     }
  33.     
  34.     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  35. }
  36.  
  37.  
  38. /*
  39. ==============
  40. COM_Parse
  41.  
  42. Parse a token out of a string
  43. ==============
  44. */
  45. char *COM_Parse (char *data)
  46. {
  47.     int        c;
  48.     int        len;
  49.     
  50.     len = 0;
  51.     com_token[0] = 0;
  52.     
  53.     if (!data)
  54.         return NULL;
  55.         
  56. // skip whitespace
  57. skipwhite:
  58.     while ( (c = *data) <= ' ')
  59.     {
  60.         if (c == 0)
  61.         {
  62.             com_eof = true;
  63.             return NULL;            // end of file;
  64.         }
  65.         data++;
  66.     }
  67.     
  68. // skip // comments
  69.     if (c=='/' && data[1] == '/')
  70.     {
  71.         while (*data && *data != '\n')
  72.             data++;
  73.         goto skipwhite;
  74.     }
  75.     
  76.  
  77. // handle quoted strings specially
  78.     if (c == '\"')
  79.     {
  80.         data++;
  81.         do
  82.         {
  83.             c = *data++;
  84.             if (c=='\"')
  85.             {
  86.                 com_token[len] = 0;
  87.                 return data;
  88.             }
  89.             com_token[len] = c;
  90.             len++;
  91.         } while (1);
  92.     }
  93.  
  94. // parse single characters
  95.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  96.     {
  97.         com_token[len] = c;
  98.         len++;
  99.         com_token[len] = 0;
  100.         return data+1;
  101.     }
  102.  
  103. // parse a regular word
  104.     do
  105.     {
  106.         com_token[len] = c;
  107.         data++;
  108.         len++;
  109.         c = *data;
  110.     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  111.             break;
  112.     } while (c>32);
  113.     
  114.     com_token[len] = 0;
  115.     return data;
  116. }
  117.  
  118.  
  119.  
  120.  
  121. /*
  122. ================
  123. filelength
  124. ================
  125. */
  126. int filelength (int handle)
  127. {
  128.     struct stat    fileinfo;
  129.     
  130.     if (fstat (handle,&fileinfo) == -1)
  131.     {
  132.         Error ("Error fstating");
  133.     }
  134.  
  135.     return fileinfo.st_size;
  136. }
  137.  
  138. int tell (int handle)
  139. {
  140.     return lseek (handle, 0, SEEK_CUR);
  141. }
  142.  
  143. char *strupr (char *start)
  144. {
  145.     char    *in;
  146.     in = start;
  147.     while (*in)
  148.     {
  149.         *in = toupper(*in);
  150.         in++;
  151.     }
  152.     return start;
  153. }
  154.  
  155. char *strlower (char *start)
  156. {
  157.     char    *in;
  158.     in = start;
  159.     while (*in)
  160.     {
  161.         *in = tolower(*in);
  162.         in++;
  163.     }
  164.     return start;
  165. }
  166.  
  167.  
  168. /*
  169. =============================================================================
  170.  
  171.                         MISC FUNCTIONS
  172.  
  173. =============================================================================
  174. */
  175.  
  176. /*
  177. =================
  178. Error
  179.  
  180. For abnormal program terminations
  181. =================
  182. */
  183. void Error (char *error, ...)
  184. {
  185.     va_list argptr;
  186.  
  187.     printf ("\n************ ERROR ************\n");
  188.  
  189.     va_start (argptr,error);
  190.     vprintf (error,argptr);
  191.     va_end (argptr);
  192.     printf ("\n");
  193.     exit (1);
  194. }
  195.  
  196.  
  197. /*
  198. =================
  199. CheckParm
  200.  
  201. Checks for the given parameter in the program's command line arguments
  202. Returns the argument number (1 to argc-1) or 0 if not present
  203. =================
  204. */
  205. int CheckParm (char *check)
  206. {
  207.     int             i;
  208.  
  209.     for (i = 1;i<myargc;i++)
  210.     {
  211.         if ( !strcasecmp(check, myargv[i]) )
  212.             return i;
  213.     }
  214.  
  215.     return 0;
  216. }
  217.  
  218.  
  219. #ifndef O_BINARY
  220. #define O_BINARY 0
  221. #endif
  222.  
  223. int SafeOpenWrite (char *filename)
  224. {
  225.     int     handle;
  226.  
  227.     umask (0);
  228.     
  229.     handle = open(filename,O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
  230.     , 0666);
  231.  
  232.     if (handle == -1)
  233.         Error ("Error opening %s: %s",filename,strerror(errno));
  234.  
  235.     return handle;
  236. }
  237.  
  238. int SafeOpenRead (char *filename)
  239. {
  240.     int     handle;
  241.  
  242.     handle = open(filename,O_RDONLY | O_BINARY);
  243.  
  244.     if (handle == -1)
  245.         Error ("Error opening %s: %s",filename,strerror(errno));
  246.  
  247.     return handle;
  248. }
  249.  
  250.  
  251. void SafeRead (int handle, void *buffer, long count)
  252. {
  253.     if (read (handle,buffer,count) != count)
  254.         Error ("File read failure");
  255. }
  256.  
  257.  
  258. void SafeWrite (int handle, void *buffer, long count)
  259. {
  260.     if (write (handle,buffer,count) != count)
  261.         Error ("File write failure");
  262. }
  263.  
  264.  
  265. void *SafeMalloc (long size)
  266. {
  267.     void *ptr;
  268.  
  269.     ptr = malloc (size);
  270.  
  271.     if (!ptr)
  272.         Error ("Malloc failure for %lu bytes",size);
  273.  
  274.     return ptr;
  275. }
  276.  
  277.  
  278. /*
  279. ==============
  280. LoadFile
  281. ==============
  282. */
  283. long    LoadFile (char *filename, void **bufferptr)
  284. {
  285.     int             handle;
  286.     long    length;
  287.     void    *buffer;
  288.  
  289.     handle = SafeOpenRead (filename);
  290.     length = filelength (handle);
  291.     buffer = SafeMalloc (length+1);
  292.     ((byte *)buffer)[length] = 0;
  293.     SafeRead (handle, buffer, length);
  294.     close (handle);
  295.  
  296.     *bufferptr = buffer;
  297.     return length;
  298. }
  299.  
  300.  
  301. /*
  302. ==============
  303. SaveFile
  304. ==============
  305. */
  306. void    SaveFile (char *filename, void *buffer, long count)
  307. {
  308.     int             handle;
  309.  
  310.     handle = SafeOpenWrite (filename);
  311.     SafeWrite (handle, buffer, count);
  312.     close (handle);
  313. }
  314.  
  315.  
  316.  
  317. void DefaultExtension (char *path, char *extension)
  318. {
  319.     char    *src;
  320. //
  321. // if path doesn't have a .EXT, append extension
  322. // (extension should include the .)
  323. //
  324.     src = path + strlen(path) - 1;
  325.  
  326.     while (*src != PATHSEPERATOR && src != path)
  327.     {
  328.         if (*src == '.')
  329.             return;                 // it has an extension
  330.         src--;
  331.     }
  332.  
  333.     strcat (path, extension);
  334. }
  335.  
  336.  
  337. void DefaultPath (char *path, char *basepath)
  338. {
  339.     char    temp[128];
  340.  
  341.     if (path[0] == PATHSEPERATOR)
  342.         return;                   // absolute path location
  343.     strcpy (temp,path);
  344.     strcpy (path,basepath);
  345.     strcat (path,temp);
  346. }
  347.  
  348.  
  349. void    StripFilename (char *path)
  350. {
  351.     int             length;
  352.  
  353.     length = strlen(path)-1;
  354.     while (length > 0 && path[length] != PATHSEPERATOR)
  355.         length--;
  356.     path[length] = 0;
  357. }
  358.  
  359. void    StripExtension (char *path)
  360. {
  361.     int             length;
  362.  
  363.     length = strlen(path)-1;
  364.     while (length > 0 && path[length] != '.')
  365.     {
  366.         length--;
  367.         if (path[length] == '/')
  368.             return;        // no extension
  369.     }
  370.     if (length)
  371.         path[length] = 0;
  372. }
  373.  
  374.  
  375. /*
  376. ====================
  377. Extract file parts
  378. ====================
  379. */
  380. void ExtractFilePath (char *path, char *dest)
  381. {
  382.     char    *src;
  383.  
  384.     src = path + strlen(path) - 1;
  385.  
  386. //
  387. // back up until a \ or the start
  388. //
  389.     while (src != path && *(src-1) != PATHSEPERATOR)
  390.         src--;
  391.  
  392.     memcpy (dest, path, src-path);
  393.     dest[src-path] = 0;
  394. }
  395.  
  396. void ExtractFileBase (char *path, char *dest)
  397. {
  398.     char    *src;
  399.  
  400.     src = path + strlen(path) - 1;
  401.  
  402. //
  403. // back up until a \ or the start
  404. //
  405.     while (src != path && *(src-1) != PATHSEPERATOR)
  406.         src--;
  407.  
  408.     while (*src && *src != '.')
  409.     {
  410.         *dest++ = *src++;
  411.     }
  412.     *dest = 0;
  413. }
  414.  
  415. void ExtractFileExtension (char *path, char *dest)
  416. {
  417.     char    *src;
  418.  
  419.     src = path + strlen(path) - 1;
  420.  
  421. //
  422. // back up until a . or the start
  423. //
  424.     while (src != path && *(src-1) != '.')
  425.         src--;
  426.     if (src == path)
  427.     {
  428.         *dest = 0;    // no extension
  429.         return;
  430.     }
  431.  
  432.     strcpy (dest,src);
  433. }
  434.  
  435.  
  436. /*
  437. ==============
  438. ParseNum / ParseHex
  439. ==============
  440. */
  441. long ParseHex (char *hex)
  442. {
  443.     char    *str;
  444.     long    num;
  445.  
  446.     num = 0;
  447.     str = hex;
  448.  
  449.     while (*str)
  450.     {
  451.         num <<= 4;
  452.         if (*str >= '0' && *str <= '9')
  453.             num += *str-'0';
  454.         else if (*str >= 'a' && *str <= 'f')
  455.             num += 10 + *str-'a';
  456.         else if (*str >= 'A' && *str <= 'F')
  457.             num += 10 + *str-'A';
  458.         else
  459.             Error ("Bad hex number: %s",hex);
  460.         str++;
  461.     }
  462.  
  463.     return num;
  464. }
  465.  
  466.  
  467. long ParseNum (char *str)
  468. {
  469.     if (str[0] == '$')
  470.         return ParseHex (str+1);
  471.     if (str[0] == '0' && str[1] == 'x')
  472.         return ParseHex (str+2);
  473.     return atol (str);
  474. }
  475.  
  476.  
  477.  
  478. /*
  479. ============================================================================
  480.  
  481.                     BYTE ORDER FUNCTIONS
  482.  
  483. ============================================================================
  484. */
  485.  
  486. #ifdef __BIG_ENDIAN__
  487.  
  488. short   LittleShort (short l)
  489. {
  490.     byte    b1,b2;
  491.  
  492.     b1 = l&255;
  493.     b2 = (l>>8)&255;
  494.  
  495.     return (b1<<8) + b2;
  496. }
  497.  
  498. short   BigShort (short l)
  499. {
  500.     return l;
  501. }
  502.  
  503.  
  504. long    LittleLong (long l)
  505. {
  506.     byte    b1,b2,b3,b4;
  507.  
  508.     b1 = l&255;
  509.     b2 = (l>>8)&255;
  510.     b3 = (l>>16)&255;
  511.     b4 = (l>>24)&255;
  512.  
  513.     return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  514. }
  515.  
  516. long    BigLong (long l)
  517. {
  518.     return l;
  519. }
  520.  
  521.  
  522. float    LittleFloat (float l)
  523. {
  524.     union {byte b[4]; float f;} in, out;
  525.     
  526.     in.f = l;
  527.     out.b[0] = in.b[3];
  528.     out.b[1] = in.b[2];
  529.     out.b[2] = in.b[1];
  530.     out.b[3] = in.b[0];
  531.     
  532.     return out.f;
  533. }
  534.  
  535. float    BigFloat (float l)
  536. {
  537.     return l;
  538. }
  539.  
  540.  
  541. #else
  542.  
  543.  
  544. short   BigShort (short l)
  545. {
  546.     byte    b1,b2;
  547.  
  548.     b1 = l&255;
  549.     b2 = (l>>8)&255;
  550.  
  551.     return (b1<<8) + b2;
  552. }
  553.  
  554. short   LittleShort (short l)
  555. {
  556.     return l;
  557. }
  558.  
  559.  
  560. long    BigLong (long l)
  561. {
  562.     byte    b1,b2,b3,b4;
  563.  
  564.     b1 = l&255;
  565.     b2 = (l>>8)&255;
  566.     b3 = (l>>16)&255;
  567.     b4 = (l>>24)&255;
  568.  
  569.     return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  570. }
  571.  
  572. long    LittleLong (long l)
  573. {
  574.     return l;
  575. }
  576.  
  577. float    BigFloat (float l)
  578. {
  579.     union {byte b[4]; float f;} in, out;
  580.     
  581.     in.f = l;
  582.     out.b[0] = in.b[3];
  583.     out.b[1] = in.b[2];
  584.     out.b[2] = in.b[1];
  585.     out.b[3] = in.b[0];
  586.     
  587.     return out.f;
  588. }
  589.  
  590. float    LittleFloat (float l)
  591. {
  592.     return l;
  593. }
  594.  
  595.  
  596.  
  597. #endif
  598.  
  599.